package utils.effects
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.filters.ColorMatrixFilter;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	

	public class CD
	{ 
		private static var _instance:CD;
//		private static var timer:Timer; 
		
		private static var cds:Dictionary;
		private static var lockObjDic:Dictionary;
		private static var tims:Dictionary;
		private static var bgParentDic:Dictionary;
		
//		public static const grayFilter:ColorMatrixFilter = new ColorMatrixFilter([1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 0,0,0,1,0]);
		
		
		public function CD()
		{
			cds = new Dictionary();
			lockObjDic = new Dictionary();
			tims = new Dictionary();
			bgParentDic = new Dictionary();
		}
		
		private static function getInstance():CD
		{
			if(_instance == null)
			{
				_instance = new CD();
				
			}
				
			return _instance;
		}
		
		/**
		 * 
		 * @param displayObj: 被遮罩物品
		 * @param cdSecondTime：遮罩时间。单位秒
		 * @param bg：遮罩背景, 如果为空，则使用默认黑色半透明背景
		 * 
		 */			
		private static const size:int=200;//粒度，粒度越小越好
		public static function addCD(displayObj:MovieClip, cdSecondTime:uint, lockObj:MovieClip=null, bg:DisplayObject=null):void
		{
			getInstance();
			lockObj.mouseEnabled = false;
			lockObj.mouseChildren = false;
			lockObj.filters = [new ColorMatrixFilter([1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 0,0,0,1,0])];
			var angle:Number=0;
			var cishu:int=cdSecondTime*1000/size;
			var timer:Timer=new Timer(size,cishu);
			timer.addEventListener(TimerEvent.TIMER,angleChange);
			timer.addEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
			tims[timer] = timer;
			
			lockObjDic[timer] = lockObj;

			var bgParent:MovieClip = new MovieClip();
			if(displayObj==null){
				var bgRep:Sprite=new Sprite();
				bgRep.graphics.beginFill(0x660000);
				bgRep.graphics.drawRect(0,0,lockObj.width,lockObj.height);
				bgRep.graphics.endFill();
				bgRep.alpha=0.6;
				bgParent.addChild(bgRep);
			}
			else{
				bgParent.addChild(displayObj);
			}
			
			
//			displayObj.addChildAt(bgParent,displayObj.numChildren);
//			bgParent.x=displayObj.x; 
//			bgParent.y=displayObj.y;
//			bgParentDic[timer] = bgParent;
//			var label:TextField = new TextField();
//			label.autoSize = TextFieldAutoSize.CENTER;
//			label.background = false;
//			label.border = false;
			
//			var format:TextFormat = new TextFormat();
//			format.font = "Verdana";
//			format.color = 0xFF0000;
//			format.size = 15;
//			label.defaultTextFormat=format;
//			label.x=displayObj.x+displayObj.width/2;
//			label.y=displayObj.x+displayObj.height/2;
//			displayObj.parent.addChild(label);
			
//			label.defaultTextFormat = format;
//			displayObj.parent.addChild(label);
			
			var Msk:Sprite=new Sprite();
//			Msk.filters = [new ColorMatrixFilter([1,0,0,0,0, 1,0,0,0,0, 1,0,0,0,0, 0,0,0,1,0])];
			Msk.alpha = 0.5;
			lockObj.addChild(Msk);
			lockObj.addChild(bgParent);
//			lockObj.addChild(bgParent);
			cds[timer] = Msk;
			
			var ceil:Number=360/cishu;
			var distance:Number=360/cdSecondTime*1000;
			
			
			
//			Tool.squareCifang(displayObj.width/2);
//			var radius:Number=Math.max(displayObj.width,displayObj.height);
			var rect:Rectangle=displayObj.getBounds(displayObj);
			var radius:Number=Math.sqrt(rect.width/2*rect.width/2+rect.height/2*rect.height/2);
			Msk.x=/*displayObj.x*/rect.width/2;
			Msk.y=/*displayObj.y*/rect.height/2;
			Msk.mask=displayObj;
//			displayObj.addEventListener(Event.ENTER_FRAME, angleChange);
			
			startCD();
			
			function angleChange(evt:TimerEvent=null):void
			{
					angle=angle+ceil;
//					trace("angle",angle,timer.currentCount,(int((timer.currentCount*size)/1000)));
					angle %= 360;
					drawshape(radius,angle, evt);
//				label.text=Math.ceil((cdSecondTime*1000-timer.currentCount*size)/1000)+'';
			}
			
			function onTimerOver(evt:TimerEvent=null):void
			{
				var timer:Timer = evt.currentTarget as Timer;
				
//				if(displayObj == null)
//					return;
				var msk:Sprite = cds[timer] as Sprite;
				lockObjDic[timer].mouseEnabled = true;
				lockObjDic[timer].mouseChildren = true;
				lockObjDic[timer].filters = [];
				if(msk.parent)
					msk.parent.removeChild(msk);
				timer.removeEventListener(TimerEvent.TIMER_COMPLETE,onTimerOver);
				timer.removeEventListener(TimerEvent.TIMER,angleChange);
				timer.stop();
				
				var bg:DisplayObject = bgParentDic[timer] as DisplayObject;
//				if(bg.parent)
//					bg.parent.removeChild(bg);
				delete bgParentDic[timer];
				delete tims[timer];
				delete cds[timer];
				delete lockObjDic[timer];
			}
			
			function drawshape(radius:Number, angle:Number, evt:TimerEvent):void{
				Msk.graphics.clear(); 
				Msk.graphics.beginFill(0x000000);
				Msk.graphics.lineStyle(1); 
				Msk.graphics.lineTo(radius,0); 
				for (var i:int = 0; i <= 360-angle; ) { 
					var degree:Number = Math.PI / 180 * i; 
					var targetPoint:Point = Point.polar(radius,-degree); 
					Msk.graphics.lineTo(targetPoint.x, targetPoint.y); 
					i=i+1;
				} 
//				dealAnlge(mask,angle,rect);
				Msk.graphics.lineTo(0,0); 
				Msk.graphics.endFill();
//				Mask.mask=displayObj;
//				bgParentDic[evt.currentTarget].mask=mask;
			}
		}
		
		
		public static function startCD():void
		{
			for each(var i:Timer in tims)
			{
				i.start();
			}
			
		}
		
		public static function pauseCD():void
		{
				for each(var i:Timer in tims)
				{
					i.stop();
				}
		}
		
		public static function removeAllCD():void
		{
			var bgObj:Sprite;
			for each(bgObj in bgParentDic)
			{
				if(bgObj.parent)
					bgObj.parent.removeChild(bgObj);
			}
			
			for each(bgObj in cds)
			{
				if(bgObj.parent)
					bgObj.parent.removeChild(bgObj);
			}
			
			for each(bgObj in lockObjDic)
			{
				bgObj.mouseEnabled = true;
				bgObj.mouseChildren = true;
				bgObj.filters = [];
			}
			
			
		}
		
		private function dealAngle(mask:Shape,angle:Number,rect:Rectangle):void{
			if(angle<=45){
//				mask.graphics.lineTo(
			}
			else if(angle>45 && angle<=135){
				
			}
			else if(angle>45 && angle<=135){
				
			}
			else if(angle>135 && angle<=225){
				
			}
			else if(angle>225 && angle<=315){
				
			}
			else{
				
			}
			
		}
		
		
	}
}